﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace zevenseas.Community.Search
{
    public class WhereStatement
    {
        private List<WhereGroup> whereGroups;
        private string keywords;
        private SearchTypes keywordsType;
        private List<string> scopes;

        public List<WhereGroup> WhereGroups
        {
            get { return whereGroups; }
            set { whereGroups = value; }
        }

        public List<string> Scopes
        {
            get { return scopes; }
            set { scopes = value; }
        }

        public string Keywords
        {
            get { return keywords; }
            set { keywords = value; }
        }

        public SearchTypes KeywordsType
        {
            get { return keywordsType; }
            set { keywordsType = value; }
        }

        public WhereStatement()
        {
            whereGroups = new List<WhereGroup>();
        }

        private string GetScopeStatement()
        {
            if (scopes.Count < 1)
                return string.Empty;

            StringBuilder scopeStatement = new StringBuilder();

            scopeStatement.Append("(");
            foreach (string scope in Scopes)
            {
                scopeStatement.Append(@" (""SCOPE"" = '");
                scopeStatement.Append(scope);
                scopeStatement.Append(@"') OR");
            }

            scopeStatement.Remove(scopeStatement.Length - 2, 2);
            scopeStatement.Append(") ");
            return scopeStatement.ToString();
        }

        private string GetKeywordStatement()
        {
            StringBuilder keywordStatement = new StringBuilder();

            switch (KeywordsType)
            {
                case SearchTypes.Contains:
                    keywordStatement.Append(@"CONTAINS ('""");
                    keywordStatement.Append(Keywords);
                    keywordStatement.Append(@"""')");
                    break;
                case SearchTypes.FreeText:
                    keywordStatement.Append(@"FREETEXT(DefaultProperties, '");
                    keywordStatement.Append(Keywords);
                    keywordStatement.Append(@"')");
                    break;
            }
           
            return keywordStatement.ToString();
        }

        public override string ToString()
        {
            StringBuilder whereStatement = new StringBuilder();

            if (whereGroups.Count == 0 && string.IsNullOrEmpty(keywords) && scopes.Count == 0)
                return whereStatement.ToString();

            whereStatement.Append(@"WHERE ");
            whereStatement.Append(GetScopeStatement());

            if (!string.IsNullOrEmpty(GetScopeStatement()) && !String.IsNullOrEmpty(Keywords)) whereStatement.Append(" AND ");

            if (!String.IsNullOrEmpty(Keywords))
            {
                whereStatement.Append(GetKeywordStatement());
            }

            if (!String.IsNullOrEmpty(Keywords) && whereGroups.Count > 0) whereStatement.Append(" AND ");

            foreach (WhereGroup whereGroup in whereGroups)
            {
                if (whereGroup.LogOperator == null)
                {
                    whereStatement.Append(whereGroup.ToString());
                }
                else
                {
                    whereStatement.Append(whereGroup.ToString(true));
                }
            }

            return whereStatement.ToString();
        }
    
    }
}